package com.demo.mqservice;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

public class MqProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public static void main(String[] args) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        sendMessage();
    }
    public static void sendMessage() throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        //创建消息生产者，指定生成组名
        DefaultMQProducer mqProducer = new DefaultMQProducer("wenhao-group");
        //指定nameserver地址
//        mqProducer.setNamesrvAddr("127.0.0.1:9876;127.0.0.1:9877");
        mqProducer.setNamesrvAddr("111.229.120.57:9876;120.24.85.120:9876");
        //启动生产者
        mqProducer.start();
        //构建消息对象，主要是设置消息的主题、标签、内容
        for (int i = 0; i < 100; i++) {
            Message msg = new Message("topicTest","tagA",("hello" + i).getBytes());
            //发送消息
            SendResult result = mqProducer.send(msg);
            System.out.println("发送结果：" + result);
        }
        //关闭生产者
        mqProducer.shutdown();
    }
}
